﻿namespace Pub.Class
{
    using System;
    using System.Data;

    public class ExcelReader : Disposable
    {
        private readonly IExcelReader excelReader;

        public ExcelReader(string excelPath)
        {
            if (this.excelReader.IsNull())
            {
                this.excelReader = (IExcelReader) (WebConfig.GetApp("ExcelReaderProviderName") ?? "Pub.Class.Excel.OleDb.ExcelReader,Pub.Class.Excel.OleDb").LoadClass();
                this.excelReader.Open(excelPath);
            }
        }

        public ExcelReader(string classNameAndAssembly, string excelPath)
        {
            if (this.excelReader.IsNull())
            {
                this.excelReader = (IExcelReader) classNameAndAssembly.IfNullOrEmpty("Pub.Class.Excel.OleDb.ExcelReader,Pub.Class.Excel.OleDb").LoadClass();
                this.excelReader.Open(excelPath);
            }
        }

        public ExcelReader(string dllFileName, string className, string excelPath)
        {
            if (this.excelReader.IsNull())
            {
                this.excelReader = (IExcelReader) dllFileName.LoadClass(className);
                this.excelReader.Open(excelPath);
            }
        }

        protected override void InternalDispose()
        {
            this.excelReader.Dispose();
            base.InternalDispose();
        }

        public DataSet ToDataSet()
        {
            return this.excelReader.ToDataSet();
        }

        public DataTable ToDataTable()
        {
            return this.excelReader.ToDataTable(0);
        }

        public DataTable ToDataTable(int i)
        {
            return this.excelReader.ToDataTable(i);
        }

        public DataTable ToDataTable(string table)
        {
            return this.excelReader.ToDataTable(table);
        }
    }
}

